package com.tbit.uqbike.client.service.impl;

import cn.hutool.core.lang.PatternPool;
import com.tbit.uqbike.client.dao.core.InvoiceTitleDao;
import com.tbit.uqbike.client.pojo.InvoiceTitle;
import com.tbit.uqbike.client.pojo.Result;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.pojo.dto.AddInvoiceTitleDTO;
import com.tbit.uqbike.client.pojo.dto.UpdateInvoiceTitleDTO;
import com.tbit.uqbike.client.service.InvoiceTitleService;
import com.tbit.uqbike.client.service.TokenService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @Author:BUGTian
 * @DATE: 2023/9/4
 */
@Service
public class InvoiceTitleServiceImpl implements InvoiceTitleService {

    @Autowired
    private InvoiceTitleDao invoiceTitleDao;
    @Autowired
    private TokenService tokenService;

    @Override
    public Result<List<InvoiceTitle>> getInvoiceTitle(String token) {
        User user = tokenService.getUser(token);
        List<InvoiceTitle> invoiceTitleList = invoiceTitleDao.getByUserId(user.getUserId());
        return Result.success(invoiceTitleList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result addInvoiceTitle(AddInvoiceTitleDTO addInvoiceTitleDTO, String token) {
        if (!StringUtils.hasText(addInvoiceTitleDTO.getCompanyTitle()) || addInvoiceTitleDTO.getTitleType() == null) {
            return Result.error("抬头不能为空");
        }
        if (!StringUtils.hasText(addInvoiceTitleDTO.getEmail())) {
            return Result.error("邮箱不能为空");
        }
        if (!PatternPool.EMAIL.matcher(addInvoiceTitleDTO.getEmail()).matches()) {
            return Result.error("邮箱地址不正确");
        }
        User user = tokenService.getUser(token);
        Integer count = invoiceTitleDao.count(user.getUserId());
        if (count > 50) {
            //防止恶意添加数据
            return Result.error("添加数量超限制,请进行修改或删除");
        }
        InvoiceTitle invoiceTitle = initInvoiceTitle(addInvoiceTitleDTO, user.getUserId(), addInvoiceTitleDTO.isPriority());
        invoiceTitleDao.addInvoiceTitle(invoiceTitle);
        return Result.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result updateInvoiceTitle(UpdateInvoiceTitleDTO updateInvoiceTitleDTO, String token) {
        if (!StringUtils.hasText(updateInvoiceTitleDTO.getCompanyTitle()) || updateInvoiceTitleDTO.getTitleType() == null) {
            return Result.error("抬头不能为空");
        }
        if (!StringUtils.hasText(updateInvoiceTitleDTO.getEmail())) {
            return Result.error("邮箱不能为空");
        }
        if (!PatternPool.EMAIL.matcher(updateInvoiceTitleDTO.getEmail()).matches()) {
            return Result.error("邮箱地址不正确");
        }
        User user = tokenService.getUser(token);
        InvoiceTitle invoiceTitle = initInvoiceTitle(updateInvoiceTitleDTO, user.getUserId(), updateInvoiceTitleDTO.isPriority());
        invoiceTitleDao.updateInvoiceTitle(invoiceTitle);
        return Result.success();
    }

    @Override
    public Result deleteInvoiceTitle(String token, Integer invoiceTitleId) {
        User user = tokenService.getUser(token);
        invoiceTitleDao.deleteInvoiceTitle(user.getUserId(), invoiceTitleId);
        return Result.success();
    }

    private InvoiceTitle initInvoiceTitle(Object o, Integer userId, boolean isPriority) {
        if (isPriority) {
            //是默认的,把其他的修改为非默认
            invoiceTitleDao.reloadPriority(userId);
        }
        InvoiceTitle invoiceTitle = new InvoiceTitle();
        BeanUtils.copyProperties(o, invoiceTitle);
        invoiceTitle.setUpdateTime(new Date())
                .setUserId(userId);
        return invoiceTitle;
    }
}
